package hot100.哈希;

import java.util.*;
import java.util.concurrent.CompletionService;

/**字母异位词分组

/**
 * 给你一个字符串数组，将其中字符串内容一致的作为数值存放然后放到一个大数值里面去
 * 输入: strs = ["eat", "tea", "tan", "ate", "nat", "bat"]
 * 输出: [["bat"],["nat","tan"],["ate","eat","tea"]]
 *
 * 边入集合时就可以分类 无需全部入集合再基于此集合进行操作 (hot100.哈希.findChain128)
 * operation: 1string排序 2computeIfAbsent-compute 3视图机制 java.util.HashMap#values()-new Values()
 *
 */
public class misplaceWordStringList49 {

    public List<List<String>> method(String[] words) {
        //Map<Character[], List<String>> resultMap = new HashMap<String, List<String>>(); map 要基于泛型对于的hash和equal进行操作 char[]没有
        Map<String, List<String>> resultMap = new HashMap<>();

        for (String word : words) {
            char[] charArray = word.toCharArray();
            Arrays.sort(charArray);
            String key = new String(charArray);

            /*List<String> now = resultMap.get(key);
            if (now == null) {
                List<String> newValue = new ArrayList<>();
                newValue.add(word);
                resultMap.put(key, newValue);
            } else {
                now.add(word);
                resultMap.put(key, now);
            }*/
            resultMap.computeIfAbsent(key, value -> new ArrayList<>()).add(word);
        }
        return new ArrayList<>(resultMap.values());
    }

    public static void main(String[] args) {
        misplaceWordStringList49 a = new misplaceWordStringList49();
        System.out.println(a.method(new String[]{"eat", "tea", "tan", "ate", "nat", "bat"}));
    }

}
